package com.longtcp.server;

/**
 * Author: <黄书根>
 * Time: 2017-08-07 16:49
 * Copyright (C) 2017 Xiamen Yaxon Networks CO.,LTD.
 */
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 *
 * 上次未实现功能：没有判断一个稳定连接的客户端（可通过几次握手成功确定，几次握手成功后在将用户放入服务端缓存），
 *
 * Created by orange on 16/6/8.
 *
 */

public class TCPLongConnectionServer {
    private static boolean isStart=true;
    private static ServerResponseTask serverResponseTask;


    public TCPLongConnectionServer(){

    }


    public static void main(String[] args){

        ServerSocket serverSocket=null;
        ExecutorService executorService=Executors.newCachedThreadPool();
        try {
            serverSocket=new ServerSocket(9006);
            while (isStart) {
                Socket socket = serverSocket.accept();
                String userIP=socket.getInetAddress().getHostAddress();
                System.out.println("用户的IP地址为：" + userIP);






                serverResponseTask = new ServerResponseTask(socket,
                        new TCPLongConnectServerHandlerData.TCPResultCallBack() {

                            @Override
                            public void targetIsOffline(ChatMsgProtocol reciveMsg) {// 对方不在线
                                // 新加入的客户端成功连接后
                                if (reciveMsg != null) {
                                    // 不在线就存入离线数据库中

                                    System.out.println(reciveMsg.getMsgTargetUUID()+" is offLine");
                                }
                                else{
                                    System.out.println(reciveMsg.getMsgTargetUUID()+" onLine");
                                }
                            }

                            @Override
                            public void targetIsOnline(String targetUUId,Socket socket) throws IOException {
                               // socket.close();
                                System.out.println(targetUUId+" is onLine");
                            }
                        },userIP);

                if (socket.isConnected()) {
                    executorService.execute(serverResponseTask);


                    //serverSocket.close();
                 //   new Thread(new Keep()).start();
                }
                // printAllClient();
            }
            serverSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (serverSocket!=null){
                try {
                    isStart=false;
                    serverSocket.close();
                    if(serverSocket!=null)
                        serverResponseTask.stop();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }






    public boolean isOffLineMessageEmpty(){
        boolean isEmpty=true;

        if (isEmpty) {

        }
        return isEmpty;
    }
}